package cn.handong.tool.重要实验;

import org.junit.Test;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;

public class 测试三大循环执行速度 {
    /**
     * 测试结果：
     * for ~= foreach > lambda
     */
    @Test
    public void test() throws InterruptedException {
        List<Double> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(Math.random());
        }
        StopWatch myWatch = new StopWatch("myWatch");
        myWatch.start("for");
        int size = list.size();
        for (int i = 0; i < size; i++) {
            Double d = list.get(i);
            Double temp = d * d;
        }
        myWatch.stop();
        myWatch.start("foreach");
        for (Double li : list) {
            Double temp = li * li;
        }
        myWatch.stop();
        myWatch.start("lambda");
        list.forEach(li -> {
            Double temp = li * li;
        });
        myWatch.stop();
        System.out.println(myWatch.prettyPrint());
    }

}
